package com.zhf.JDBC03;

import com.zhf.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/9/23 21:49
 */
public class TestLogin01 {

    /**
     * 用户登录案例
     * @param args
     */
    public static void main(String[] args) throws SQLException {

        //获取连接
        Connection connection = JDBCUtils.getConnection();

        //获取statement对象
        Statement statement = connection.createStatement();

        //用户输入用户名和密码
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String usename=sc.nextLine();
        System.out.println("请输入密码：");
        String password=sc.nextLine();

        //拼接SQL语句
        String sql="select * from jdbc_user where username='"+usename+"' and password='"+password+"';";

        //执行查询
        ResultSet resultSet = statement.executeQuery(sql);

        //处理结果集
        if (resultSet.next()){
            System.out.println("欢迎登录"+usename);
        }else {
            System.out.println("登录失败");
        }

        //关闭流
        JDBCUtils.close(connection,statement,resultSet);
    }
}
